package com.dgshanger.sanhxiaofeisc.ui.person

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.App.Companion.application
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.common.Constants.KEY_INTENT_EXTRA_ID
import com.dgshanger.sanhxiaofeisc.common.Constants.KEY_INTENT_MSG_ID
import com.dgshanger.sanhxiaofeisc.entity.MessageEntity
import com.dgshanger.sanhxiaofeisc.entity.PayForDetailEntity
import com.dgshanger.sanhxiaofeisc.ui.WebActivity
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import kotlinx.android.synthetic.main.item_message_list.view.*


/**
 * MessageListFragment
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/18 15:49.
 */
class MessageListFragment : BaseMvpRecycleViewFragment<MessageEntity>() {
    override val listLimit: Int
        get() = 5

    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun initEvent(view: View?) {
        super.initEvent(view)
        adapter?.setOnItemClickListener { position, entity ->
            if (MessageEntity.TYPE_NOTIC.equals(entity.msg_type)) {
                //普通通知
                model.loadWebData(entity.msg_id, object : XxBaseHttpObserver<PayForDetailEntity>() {
                    override fun onCompleted(msg: String?, entity: PayForDetailEntity?) {
                        val intent = Intent(mContext, WebActivity::class.java)
                        intent.putExtra(Constants.KEY_DATA, entity!!.msg_content)
                        startActivity(intent)
                    }

                    override fun onError(error: String?) {
                        if (!activity!!.isFinishing) {
                            showToast(error)
                        }
                    }

                    override fun onStart() {
                        if (!activity!!.isFinishing) {
                            showLoadingDialog()
                        }
                    }

                    override fun onFinish() {
                        if (!activity!!.isFinishing) {
                            dismissLoadingDialog()
                        }
                    }
                })

            } else {
                //代付类型
                //查看详情
                val intent = Intent(mContext, PayForDetailActivity::class.java)
                intent.putExtra(KEY_INTENT_MSG_ID, entity.msg_id)
                startActivity(intent)
            }
        }
    }

    override fun createAdapter(): BaseRecycleAdapter<MessageEntity, out BaseViewHolder<MessageEntity>> = Adapter()
    private lateinit var model: Model
    override fun createModel(): BaseMvpRecycleViewContract.Model<MessageEntity> {
        model = Model()
        return model
    }

    class Model : BaseMvpRecycleViewContract.Model<MessageEntity> {

        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<MessageEntity>>?) {
            AppApi.Api()
                    .my_notice(page)
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)


        }

        fun loadWebData(msg_id: String, httpObserver: XxBaseHttpObserver<PayForDetailEntity>?) {
            AppApi.Api()
                    .notice_detail(PayForDetailEntity.TYPE_PUBLIC, msg_id)
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)
        }
    }

    class Adapter : BaseRecycleAdapter<MessageEntity, ViewHolder>() {
        override fun createViewHolder(view: View?, viewType: Int): ViewHolder = ViewHolder(view!!)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_message_list
    }

    class ViewHolder(view: View) : BaseViewHolder<MessageEntity>(view) {
        override fun bindView(position: Int, entity: MessageEntity?) {
            itemView.tv_msg_title.text = entity!!.msg_title
            itemView.tv_created_at.text = entity.created_at
            itemView.tv_msg_introduce.text = if (MessageEntity.TYPE_PAY.equals(entity.msg_type)) application.getString(R.string.Click_for_details) else entity.msg_introduce
        }

    }
}